package com.stagewebserver.webserver.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stagecommon.common.pojo.Demands;
import com.stagecommon.common.pojo.RespBean;
import com.stagecommon.common.vo.DemandsVo;
import com.stagewebserver.webserver.mapper.AuthorMapper;
import com.stagewebserver.webserver.mapper.DemandsMapper;
import com.stagecommon.common.mapper.UserMapper;
import com.stagewebserver.webserver.service.DemandsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author: yxl
 * @since: 2021-11-30
 **/
@Service
@Slf4j
public class DemandsServiceImpl extends ServiceImpl<DemandsMapper, Demands> implements DemandsService {

    @Autowired
    private DemandsMapper demandsMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AuthorMapper authorMapper;

    @Override
    public List<Demands> listDemands() {
        return demandsMapper.selectListDemands();
    }


    @Override
    public RespBean addDemands(Demands demands) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        demands.setCreatedTime(Timestamp.valueOf(simpleDateFormat.format(new Date())));
        Timestamp.valueOf(simpleDateFormat.format(demands.getClosingDate()));
        if (demandsMapper.insert(demands) == 1) {
            userMapper.incretmentDemandNum(demands.getUsername());
            if (!Objects.equals(null, authorMapper.selectAuthorByUsername(demands.getUsername()))) {
                authorMapper.incretementDemandNum(demands.getUsername());
            }
        }
        return RespBean.operationSuccess("添加成功", 200, "ok");
    }

//    @Override
//    public RespBean<List<Demands>> selectDemandsByUsername(Demands demands) {
//        return RespBean.operationSuccess("查询成功", 200, demandsMapper.selectList(new QueryWrapper<Demands>().select().eq("username", demands.getUsername())));
//    }

    @Override
    public RespBean<List<DemandsVo>> selectDemandsByUserId(DemandsVo demands) {
        return RespBean.operationSuccess("查询成功", 200, demandsMapper.selectDemandsByUserId(demands.getUserId()));
    }

    @Override
    public RespBean<DemandsVo> selectDemandDetailById(Integer demandId) {
        return RespBean.operationSuccess("查询成功", 200, demandsMapper.selectDemandById(demandId));
    }

    @Override
    public RespBean<List<DemandsVo>> getDemandByKeyword(String keyword) {
        return RespBean.operationSuccess("查询成功", 200, demandsMapper.selectDemandByKeyword(keyword));
    }

    @Override
    public RespBean<List<DemandsVo>> selectDemandByCategory(Demands demands) {
        return RespBean.operationSuccess("查询成功", 200, demandsMapper.selectDemandByCategory(demands.getCategoryName()));
    }

    @Override
    public RespBean<Integer> deleteDemandById(Demands demands) {
        if (demandsMapper.deleteById(demands) == 1) {
            userMapper.decretmentDemandNum(demands.getUsername());
            if (!Objects.equals(null, authorMapper.selectAuthorByUsername(demands.getUsername()))) {
                authorMapper.decretementDemandNum(demands.getUsername());
            }
            return RespBean.operationSuccess("删除需求成功！", 200, 1);
        }
        return RespBean.operationFail("删除需求失败！");
    }
}
